import sys
sys.path.append("/home/sivan/0_0/projects/gitee/Small_Tools/db_opr/python")
from mysql import Mysql
sys.path.append("/home/sivan/0_0/projects/gitee/Small_Tools/utils")
from request import Request
from String import String
import json
import random
from queue import Queue
import traceback
from datetime import datetime

class Main:
    def __init__(self):
        self.run_conf = self.__get_conf("run.json")
        self.tag = self.run_conf["tag"]
        self.queue = Queue()
        for i in self.run_conf["list"]:
            self.queue.put(i)
        db_info = self.__get_conf("/home/sivan/0_0/key_info/mysql_sivan.json")
        self.db = Mysql(db_info)
        self.baiduyun_info = self.__get_conf("/home/sivan/0_0/key_info/baiduyun.json")
        self.url = "https://pan.baidu.com/api/list"
    def __get_conf(self, file):
        with open(file) as f:
            src = f.read()
        return json.loads(src)
    def __get(self):
        bi = self.baiduyun_info
        # BFS广度优先搜索
        while not self.queue.empty():
            bi["headers"]["dir"] = self.queue.get(False)
            for page in range(1, 10001):
                bi["headers"]["page"] = str(page)
                bi["headers"]["t"] = str(random.random())
                m = Request.get(self.url, bi["url_params"], bi["headers"], bi["cookie"])
                String.output_proc(f'{bi["headers"]["dir"]} - {page}')
                m = json.loads(m)
                m_list = m["list"]
                if 0 == len(m_list): break
                file_list = []
                for file in m_list:
                    if 1 == file["isdir"]: self.queue.put(file["path"])
                    else: 
                        item = {}
                        item["path"] = file["path"].replace("'", "\\'")
                        item["file"] = file["server_filename"].replace("'", "\\'")
                        item["dir"] = file["path"][:len(file["path"]) - len(file["server_filename"]) - 1].replace("'", "\\'")
                        file_list.append(item)
                if 0 != len(file_list): self.__save_db(file_list)
    def __save_db(self, file_list):
        sql = "insert into bdy_path(file, dir, path, tag) values \n"
        for i in file_list:
            sql += f"('{i['file']}', '{i['dir']}', '{i['path']}', '{self.tag}'),\n"
        sql = sql[:-2] + ";"
        self.sql = sql
        status = self.db.exec(sql)
        if not status["status"]:
            print(status)
            raise Exception(str(status))
    def run(self):
        begin = datetime.now()
        try:
            self.__get()
        except Exception as e:
            print("\n" + self.sql)
            exc_type, exc_value, exc_traceback_obj = sys.exc_info()
            traceback.print_tb(exc_traceback_obj)
        finally:
            end = datetime.now()
            delt = end - begin
            print("\n    用时: " + str(delt.seconds) + "." + str(delt.microseconds) + " 秒")
            self.run_conf["list"] = []
            while not self.queue.empty():
                self.run_conf["list"].append(self.queue.get(False))
            with open("run.json", "w+") as f:
                f.write(json.dumps(self.run_conf, indent=4, ensure_ascii=False))
if "__main__" == __name__:
    m = Main()
    m.run()

